/*
 * test.c
 *
 *  Created on: 2015年10月9日
 *      Author: chens
 */

#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include "cm_log.h"

static inline int time_udiff(const struct timeval* tmStart,
        const struct timeval* tmEnd)
{
    return abs(
            (tmEnd->tv_sec - tmStart->tv_sec) * 1000000
                    + (tmEnd->tv_usec - tmStart->tv_usec));
}

int main()
{
    const char * fname = "test_write.db";
    log_setup(fname);
    char * buf;

    int i;
    struct timeval tb, tn;
    int diff;
    int max = 0;
    for (i = 0; i < 1000; i++) {
        gettimeofday(&tb, NULL);
        log_debug("index = %d", i);
        gettimeofday(&tn, NULL);
        diff = time_udiff(&tb, &tn);
//        printf("%d: %dms\n", i, diff / 1000);
        if (diff > max)
            max = diff;

        usleep(2 * 1000);
    }

    log_flush();
    printf("Max time: %dus\n", max);
    sleep(1);

    int count;
    time_t bg;
    time_t ed;
    log_query_stat(&count, &bg, &ed);
    log_query_time(bg, ed, 10, &buf);

    printf("Query result:\n%s", buf);
    log_freebuf(buf);

    getchar();
}

